﻿using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class SysMenuController : Controller
    {
        private SysMenuServers menuServers;

        public SysMenuController()
        {
            menuServers = new SysMenuServers();
        }

        // GET: System/SysMenu
        public ActionResult Index()
        {
            ViewBag.TreeData = JsonConvert.SerializeObject(GetTreeData());
            return View();
        }

        // GET: System/SysMenu/Details/5
        public ActionResult Details(int? id)
        {
            ViewBag.TreeData = GetTreeData();

            if (id != null)
            {
                ViewBag.Title = "编辑菜单";
                var model = GetMenuModel(id.Value.ToString());
                if (model != null)
                    return View(model);
            }
            return View();
        }

        // GET: System/SysMenu/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TreeData = GetTreeData();
            var model = new sys_menu();
            if (id != null)
            {
                ViewBag.Title = "编辑菜单";
                model = GetMenuModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建菜单";
            }
            return View(model);
        }

        // POST: System/SysMenu/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, sys_menu model, FormCollection formCollection)
        {
            ViewBag.TreeData = GetTreeData();
            try
            {
                if (id == null || id == 0)
                {
                    menuServers.AddMenu(model);
                    ViewBag.msg = "新增菜单信息成功！";
                    model = new sys_menu();
                }
                else
                {
                    menuServers.UpdateMenu(model);
                    ViewBag.msg = "修改菜单信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        public ActionResult AddAction(int? id)
        {
            ViewBag.MenuId = id;
            return View();
        }

        [HttpPost]
        public ActionResult AddAction(int? id, actionpermission model)
        {
            if (id != null)
            {
                model.MENU_ID = id;
                menuServers.AddAction(model);
            }

            return View();
        }

        // GET: System/SysMenu/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id != null)
            {
                menuServers.Del(id.ToString());
            }
            return Content("");
        }


        public ActionResult DeleteAction(int id)
        {
            menuServers.DelAction(id);
            return Content("");
        }

        #region //GetDate

        public string GetTableJsonResult()
        {
            //var res = new JsonResult();

            var list = menuServers.GetSys_Menus();

            //res.Data = JsonConvert.SerializeObject(list); /*person;*///返回单个对象；

            //res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。
            return JsonConvert.SerializeObject(list);
        }

        public sys_menu GetMenuModel(string id)
        {
            //var res = new JsonResult();

            var model = menuServers.GetSys_MenusById(id);

            //res.Data = JsonConvert.SerializeObject(list); /*person;*///返回单个对象；

            //res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。
            return model;
        }

        #region 绑定导航菜单=============================

        private List<SelectListItem> GetTreeData()
        {
            List<SelectListItem> menus = new List<SelectListItem>();
            var list = menuServers.GetSys_Menus();

            menus.Clear();
            menus.Add(new SelectListItem() { Value = "0", Text = "无父级导航" });

            foreach (var dr in list)
            {
                string Id = dr.MENU_ID.ToString();
                int ClassLayer = int.Parse(dr.LEVEL.ToString());
                string Title = dr.MENU_NAME.ToString().Trim();

                if (ClassLayer == 0)
                {
                    menus.Add(new SelectListItem { Value = Id, Text = Title });
                }
                else
                {
                    Title = "├ " + Title;
                    Title = StringUtil.StringOfChar(ClassLayer, "　") + Title;
                    menus.Add(new SelectListItem { Value = Id, Text = Title });
                }
            }

            return menus;
        }

        #endregion 绑定导航菜单=============================

        public string GetPermissions(string id)
        {
            var list = menuServers.GetPermissions(id);
            return JsonConvert.SerializeObject(list);
        }

        #endregion //GetDate
    }
}